package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalErrorConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.KiteAssert;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @ClassName : ContactCertNumHandler
 * @Description : 联系人证件号码
 * @Author : xie.zhengjun
 * @Date : 2020/10/30 20:26
 */
@Service
public class ContactCertNumHandler extends AbstractAttrHandler {
    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {

    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        KiteAttr contactCertType = querySceneInstService.getCommonAttr(sceneInstId, "contact_info_cardtype");
        Optional.ofNullable(contactCertType).ifPresent(kiteAttr -> {
            // 身份证15位
            if (KiteStringUtils.isEqual(kiteAttr.getValue(), "0")) {
                KiteAssert.state(value.matches("^\\d{15}$"), LocalErrorConsts.CERT_NUM_INVALID, "请输入正确的15位身份证号码");
            }
            // 身份证18位
            else if (KiteStringUtils.isEqual(kiteAttr.getValue(), "1")) {
                KiteAssert.state(value.matches("(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)"), LocalErrorConsts.CERT_NUM_INVALID,
                    "请输入正确的18位身份证号码");
            }
        });
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
